package com.experiment.experiment_big.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;

import javax.security.auth.callback.PasswordCallback;

@Configuration
public class SecurityConfigTest extends WebSecurityConfigurerAdapter {
    @Autowired
    @Qualifier(value = "my")
    private UserDetailsService userDetailsService;

    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(userDetailsService).passwordEncoder(password());
    }
    @Bean
    PasswordEncoder password(){
        return new BCryptPasswordEncoder();
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.logout().logoutUrl("/logout").logoutSuccessUrl("/tlogin").permitAll();
        http.exceptionHandling().accessDeniedPage("/403.html");
        http.formLogin()
                .loginPage("/tlogin").permitAll()  //登录页面设置
                .loginProcessingUrl("/login")
                .successForwardUrl("/tindex")//成功后
                .failureUrl("/loginerror")
                .and().authorizeRequests()
                    .antMatchers().permitAll()
                    .antMatchers("/register","/form/register").permitAll()
                    .antMatchers("/epidmic-data/*","/submitform").hasAnyAuthority("user","admin")
                    .antMatchers("/verification").permitAll()
                    .antMatchers("/multiple_conditions","/allUser","/user_multiple","/deleteUser").hasAuthority("admin")
                    .antMatchers("/high_risk","/deleteDangerRegion","/addDangerRegion").hasAuthority("admin")
                    .antMatchers("/admin_clock","/punch_in_record","/data_multiple").hasAuthority("admin")
                .anyRequest().authenticated()
                .and().csrf().disable()
                      .headers().frameOptions().disable();
     }

    @Override
    public
    void configure(WebSecurity web) throws Exception {
        web.ignoring().mvcMatchers("/css/**","/image/*","/js/**","/_MACOSX/**","/fonts/**","/plugins/**");
    }
}
